package com.penghk.springbootdemo.rabbitMQ;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

import java.io.IOException;

public class MyConsumer extends DefaultConsumer {

    Channel channel;
    /**
     * Constructs a new instance and records its association to the passed-in channel.
     *
     * @param channel the channel to which this consumer is attached
     */
    public MyConsumer(Channel channel) {
        super(channel);
        this.channel = channel;
    }


    /**
     * No-op implementation of {@link Consumer#handleDelivery}.
     *
     * @param consumerTag
     * @param envelope
     * @param properties
     * @param body
     */
    @Override
    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

        String routingKey = envelope.getRoutingKey();
        String contentType = properties.getContentType();
        long deveTag = envelope.getDeliveryTag();
        String msg = new String(body);
        System.out.println("msg : "+msg);
        channel.basicAck(deveTag,false);

    }
}
